﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JieBooLaw.WebAdmin
{
    using JieBooLaw.Common.Web.Util;
    using JieBooLaw.Common.Web;
    using JieBooLaw.Domain;
    using JieBooLaw.Service;
    using Ext.Net;
    using JieBooLaw.Common.Util;
    using JieBooLaw.Common.Security;

    public partial class ReleaseItem : BaseWebPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                this.BindData();
            }
        }

        protected void MyData_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            this.BindData();
        }

        private void BindData()
        {
            var store = this.GridPanel1.GetStore();

            store.DataSource = GetAllEntity();
            store.DataBind();

            this.BindPItem();
        }

        private IList<TInfoRelease_Item> GetAllEntity()
        {
            TInfoRelease_ItemSvc svc = new TInfoRelease_ItemSvc();
            IList<TInfoRelease_Item> list = svc.GetAllList();

            return list;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            TInfoRelease_Item item = new TInfoRelease_Item()
            {
                Active = txtActive.Checked,
                ItemDesc = txtItemDesc.Text,
                ItemID = string.IsNullOrEmpty(txtItemID.Text) ? 0 : ConvertUtil.ToInt(txtItemID.Text.Trim()),
                ItemName = txtItemName.Text,
                ItemSort = ConvertUtil.ToInt(txtItemSort.Text.Trim()),
                Level = ConvertUtil.ToInt(txtLevel.Text.Trim()),
                LinkURL = txtLinkURL.Text
            };

            if (!string.IsNullOrEmpty(txtPItemID.Text))
            {
                item.PItemID = ConvertUtil.ToInt(txtPItemID.Value);
            }

            TInfoRelease_ItemSvc svc = new TInfoRelease_ItemSvc();

            if (string.IsNullOrEmpty(txtItemID.Text))
                svc.Add(item);
            else
                svc.Save(item);

            this.BindData();
            winDetails.Hide();
        }

        protected void btnDelete_Click(object sender, DirectEventArgs e)
        {
            TInfoRelease_ItemSvc svc = new TInfoRelease_ItemSvc();

            string json = e.ExtraParams["Values"];

            Dictionary<string, string>[] companies = JSON.Deserialize<Dictionary<string, string>[]>(json);

            foreach (Dictionary<string, string> row in companies)
            {
                string id = row["ItemID"];
                svc.Delete(new TInfoRelease_Item() { ItemID = ConvertUtil.ToInt(id) });
            }

            this.BindData();
        }

        private void BindPItem()
        {
            var store = this.txtPItemID.GetStore();

            store.DataSource = GetAllEntity();
            store.DataBind();
        }
    }
}
